package com.weirong.smartvault.dao;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import com.weirong.smartvault.entity.Equipment;

@Repository
@Qualifier("equipmentRepository")
public interface EquipmentRepository extends PagingAndSortingRepository<Equipment, Integer> {

    public Equipment findEquipmentByEquipmentCode(String equipmentCode);
    
    @Query("SELECT equipment FROM Equipment equipment WHERE equipment.equipmentType=:equipmentType and equipment.equipmentCategory=:equipmentCategory")
    public Equipment findEquipmentByType(@Param("equipmentType") String equipmentType, @Param("equipmentCategory") String equipmentCategory);
    
    @Query("SELECT equipment FROM Equipment equipment WHERE equipment.equipmentType=:equipmentType and equipment.equipmentPart=:equipmentPart")
    public Equipment findEquipmentByTypeAndPart(@Param("equipmentType") String equipmentType, @Param("equipmentPart") Integer equipmentPart);
    
    @Modifying
    @Transactional
    @Query("update Equipment equipment set equipment.equipmentVersion = ?1 where equipment.equipmentType = ?2 and equipment.equipmentPart = ?3")
    public int updateEquipmentVersionByTypeAndPart(String equipmentVersion, String equipmentType, Integer equipmentPart);
    
    @Modifying
    @Transactional
    @Query("update Equipment equipment set equipment.equipmentVersion = ?1 where equipment.id = ?2")
    public int updateEquipmentVersionById(String equipmentVersion, Integer id);

    public Equipment findByEquipmentTypeAndEquipmentPart(String type,Integer part);
}
